﻿using System;
using System.Globalization;
using System.Web;
using System.Text.RegularExpressions;
using System.Configuration;
using System.IO;
using System.Net;

namespace Wild2.Models.Text
{
    public class Helper
    {
        public static string GetLastMessage()
        {
            Object o = HttpContext.Current.Session[Wild2.Models.Constants.SessionConstants.LAST_MSG];
            return (o == null) ? "" : o.ToString();
        }

        public static void SetLastMessage(String msg)
        {
            HttpContext.Current.Session[Wild2.Models.Constants.SessionConstants.LAST_MSG] = msg;
        }

        public static string CutUrl(string url)
        {
            try
            {
                //WebRequest req = WebRequest.Create("http://tinyurl.com/api-create.php?url=" + HttpUtility.UrlEncode(url));
                string path = "http://api.bit.ly/v3/shorten?login=" + ConfigurationManager.AppSettings["BitLyLogin"] + "&format=txt&apiKey=" + ConfigurationManager.AppSettings["BitLyKey"] + "&longUrl=" + HttpUtility.UrlEncode(url);
                WebRequest req = WebRequest.Create(path);
                req.Method = "GET";
                req.Timeout = 2000;
                HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
                using (Stream receivestream = resp.GetResponseStream())
                {
                    //using (StreamReader sr = new StreamReader(receivestream, System.Text.Encoding.GetEncoding(1251)))
                    if (receivestream != null)
                    {
                        using (StreamReader sr = new StreamReader(receivestream, System.Text.Encoding.UTF8))
                        {
                            url = sr.ReadToEnd();
                        }
                    }
                }
                resp.Close();
            }
// ReSharper disable EmptyGeneralCatchClause
            catch (Exception) { }
// ReSharper restore EmptyGeneralCatchClause
            return url.Trim();
        }

        //static private int GetNumber(string input)
        //{
        //    try
        //    {
        //        return int.Parse(input);
        //    }
        //    catch (FormatException)
        //    {
        //        return -1;
        //    }
        //}
        //static private string GetFontSize(int input)
        //{
        //    switch (input)
        //    {
        //        case 1:
        //            return "50%";
        //        case 2:
        //            return "70%";
        //        case 3:
        //            return "80%";
        //        case 4:
        //            return "90%";
        //        default:
        //            return "100%";
        //        case 6:
        //            return "120%";
        //        case 7:
        //            return "140%";
        //        case 8:
        //            return "160%";
        //        case 9:
        //            return "180%";
        //    }
        //    ///return string.Format("{0}pt",input*2);
        //}
        // ReSharper disable InconsistentNaming
        const RegexOptions m_options = RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.Singleline;
        static private readonly Regex engText = new Regex(@"[0-9A-z_\.\-]", m_options);
        static private readonly string domain = ConfigurationManager.AppSettings["domain"];
        static private readonly Regex r_bold = new Regex(@"\[(B|Б)\](?<inner>(.*?))\[/(B|Б)\]", m_options);
        static private readonly Regex r_red = new Regex(@"\[red\](?<inner>(.*?))\[/red\]", m_options);
        static private readonly Regex r_off = new Regex(@"\[off\](?<inner>(.*?))\[/off\]", m_options);
        static private readonly Regex r_input = new Regex(@"\[input\](?<inner>(.*?))\[/input\]", m_options);
        static private readonly Regex r_spoiler = new Regex(@"\[spoiler\](?<inner>(.*?))\[/spoiler\]", m_options);
        static private readonly Regex r_strike = new Regex(@"\[(S|С)\](?<inner>(.*?))\[/(S|С)\]", m_options);
        static private readonly Regex r_italic = new Regex(@"\[(I|И)\](?<inner>(.*?))\[/(I|И)\]", m_options);
        static private readonly Regex r_strong = new Regex(@"\[strong\](?<inner>(.*?))\[/strong\]", m_options);
        static private readonly Regex r_paragraph = new Regex(@"\[(P|П)\](?<inner>(.*?))\[/(P|П)\]", m_options);
        static private readonly Regex r_underline = new Regex(@"\[(U|Ю)\](?<inner>(.*?))\[/(U|Ю)\]", m_options);
        //static private Regex r_email2 = new Regex(@"\[email=(?<email>[^\]]*)\](?<inner>(.*?))\[/email\]", m_options);
        //static private Regex r_email1 = new Regex(@"\[email[^\]]*\](?<inner>(.*?))\[/email\]", m_options);
        static private readonly Regex r_url1 = new Regex(@"\[url\](?<http>(http://)|(https://)|(ftp://)|(ftps://))?(?<inner>(.*?))\[/url\]", m_options);
        static private readonly Regex r_url2 = new Regex(@"\[url=(?<http>(http://)|(https://)|(ftp://)|(ftps://))?(?<url>[^\]]*)\](?<inner>(.*?))\[/url\]", m_options);

        //static private readonly Regex r_urlAuto = new Regex(@"(?<![=\]])(?<http>((http(s)?|(ftp))://))(?<inner>[-a-zA-Z0-9@:;%_\+.~#?&//=]+)", m_options);

        //static private Regex r_img = new Regex(@"\[img\](?<http>(http://)|(https://)|(ftp://)|(ftps://))?(?<inner>(.*?)\.((jpg)|(gif)|(jpeg)|(bmp)|(wbmp)|(img)|(png)))\[/img\]", m_options);
        //static private Regex r_img = new Regex(@"\[img\](?<http>(http://)|(https://)|(ftp://)|(ftps://))?(?<inner>([\+!-_%А-я0-9A-z\./]*?)\.((jpg)|(gif)|(jpeg)|(bmp)|(wbmp)|(img)|(png)))\[/img\]", m_options);
        static private readonly Regex r_img = new Regex(@"\[img\](?<http>(http://)|(https://)|(ftp://)|(ftps://))?(?<inner>(.*?))\[/img\]", m_options);
        //static private Regex r_imgwild = new Regex(@"([\+!-_%А-я0-9A-z\./]*?)\.((jpg)|(gif)|(jpeg)|(bmp)|(wbmp)|(img)|(png))", m_options);
        static private readonly Regex r_youtube = new Regex(@"\[youtube\](?<http>http://)(?<inner>(www\.)?youtube.com/watch\?v=(?<id>[0-9A-Za-z-_]{11})[^[]*)\[/youtube\]", m_options);
        //static private readonly Regex r_url = new Regex(@"((?<http>(http://)|(https://)|(ftp://)|(ftps://))(?<inner>[а-яА-Я-a-zA-Z0-9@:;%_\+.~#?&//=]+))", m_options);
        
        public static string normalizeString(string s, int length)
        {
            string ss = string.Empty;
            s = s.ToLower();
            for (int i = 0; i < s.Length; i++)
            {
                char tmp = s[i];
                if (tmp == 'а') tmp = 'a';
                if (tmp == 'б') tmp = 'b';
                if (tmp == 'в') tmp = 'v';
                if (tmp == 'г') tmp = 'g';
                if (tmp == 'д') tmp = 'd';
                if (tmp == 'е') tmp = 'e';
                if (tmp == 'ё') tmp = 'e';
                if (tmp == 'ж') tmp = 'g';
                if (tmp == 'з') tmp = 'z';
                if (tmp == 'и') tmp = 'i';
                if (tmp == 'й') tmp = 'i';
                if (tmp == 'к') tmp = 'k';
                if (tmp == 'л') tmp = 'l';
                if (tmp == 'м') tmp = 'm';
                if (tmp == 'н') tmp = 'n';
                if (tmp == 'о') tmp = 'o';
                if (tmp == 'п') tmp = 'p';
                if (tmp == 'р') tmp = 'r';
                if (tmp == 'с') tmp = 's';
                if (tmp == 'т') tmp = 't';
                if (tmp == 'у') tmp = 'y';
                if (tmp == 'ф') tmp = 'f';
                if (tmp == 'х') tmp = 'h';
                if (tmp == 'ц') tmp = 'c';
                if (tmp == 'ч') tmp = 'h';
                if (tmp == 'ш') tmp = 'h';
                if (tmp == 'щ') tmp = 'h';
                if (tmp == 'ы') tmp = 'i';
                if (tmp == 'э') tmp = 'a';
                if (tmp == 'ю') tmp = 'u';
                if (tmp == 'я') tmp = 'j';
                if (engText.IsMatch(tmp.ToString(CultureInfo.InvariantCulture)))
                {
                    ss += tmp.ToString(CultureInfo.InvariantCulture);
                }
            }
            if (ss.Length <= 5)
            {
                ss = "noname" + ss;
            }
            if (ss.Length > length)
            {
                ss = ss.Substring(0, length);
            }
            return ss.ToLower();
        }

        static protected void NestedReplace(ref string refText, Regex regexMatch, string strReplace, string[] variables, string[] varDefaults)
        {
            if (variables == null) throw new ArgumentNullException("variables");
            Match m = regexMatch.Match(refText);
            while (m.Success)
            {
                string tStr = strReplace;
                int i = 0;

                foreach (string tVar in variables)
                {
                    string tValue = m.Groups[tVar].Value;
                    if (tValue.Length == 0)
                    {
                        // use default instead
                        tValue = varDefaults[i];
                    }
                    tStr = tStr.Replace("${" + tVar + "}", tValue);
                    i++;
                }

                tStr = tStr.Replace("${inner}", m.Groups["inner"].Value);

                refText = refText.Substring(0, m.Groups[0].Index) + tStr + refText.Substring(m.Groups[0].Index + m.Groups[0].Length);
                m = regexMatch.Match(refText);
            }
        }
        //For url with fixes
        static protected void NestedReplaceURL2(ref string refText, Regex regexMatch, string strReplace, string[] variables, string[] varDefaults)
        {
            MatchCollection ms = regexMatch.Matches(refText);
            foreach (Match m in ms)
            {
                string tStr = strReplace;
                int i = 0;
                string prefix = "http://";
                foreach (string tVar in variables)
                {
                    string tValue = m.Groups[tVar].Value;
                    if (tValue.Length == 0)
                    {
                        tValue = varDefaults[i];
                    }
                    if (tVar == "http") prefix = tValue;
                    tStr = tStr.Replace("${" + tVar + "}", tValue);
                    i++;
                }
                //if (m.Groups["url"].Value.Length >= 21)
                //{
                //    if (m.Groups["url"].Value.Substring(0, 21).IndexOf(ConfigurationManager.AppSettings["domainshort"], StringComparison.Ordinal) == -1)
                //    {
                //        tStr = tStr.Replace("${url}", "" + ConfigurationManager.AppSettings["domain"] + "/to/?url=" + HttpUtility.UrlEncode(prefix + m.Groups["url"].Value));
                //    }
                //    else
                //    {
                //        tStr = tStr.Replace("${url}", prefix + m.Groups["url"].Value);
                //    }
                //}
                //else
                //{
                //    if (m.Groups["url"].Value.IndexOf(ConfigurationManager.AppSettings["domainshort"], StringComparison.Ordinal) == -1)
                //    {
                //        tStr = tStr.Replace("${url}", ConfigurationManager.AppSettings["domain"] + "/to/?url=" + HttpUtility.UrlEncode(prefix + m.Groups["url"].Value));
                //    }
                //    else
                //    {
                //        tStr = tStr.Replace("${url}", prefix + m.Groups["url"].Value);
                //    }
                //}

                tStr = tStr.Replace("${url}", prefix + m.Groups["url"].Value);

                tStr = tStr.Replace("${inner}", m.Groups["inner"].Value);

                refText = refText.Replace(m.Groups[0].Value, tStr);
            }
        }

        static protected void NestedReplaceURL1(ref string refText, Regex regexMatch, string strReplace, string[] variables, string[] varDefaults)
        {
            MatchCollection ms = regexMatch.Matches(refText);
            foreach (Match m in ms)
            {
                string tStr = strReplace;
                int i = 0;
                string prefix = "http://";
                foreach (string tVar in variables)
                {
                    string tValue = m.Groups[tVar].Value;
                    if (tValue.Length == 0)
                    {
                        tValue = varDefaults[i];
                    }
                    if (tVar == "http") prefix = tValue;
                    tStr = tStr.Replace("${" + tVar + "}", tValue);
                    i++;
                }
                //if (m.Groups["inner"].Value.Length >= 21)
                //{
                //    tStr = m.Groups["inner"].Value.Substring(0, 21).IndexOf(ConfigurationManager.AppSettings["domainshort"], StringComparison.Ordinal) == -1 ? tStr.Replace("${url}", ConfigurationManager.AppSettings["domain"] + "/to/?url=" + HttpUtility.UrlEncode(prefix + m.Groups["inner"].Value)) : tStr.Replace("${url}", prefix + m.Groups["inner"].Value);
                //}
                //else
                //{
                //    tStr = m.Groups["inner"].Value.IndexOf(ConfigurationManager.AppSettings["domainshort"], StringComparison.Ordinal) == -1 ? tStr.Replace("${url}", ConfigurationManager.AppSettings["domain"] + "/to/?url=" + HttpUtility.UrlEncode(prefix + m.Groups["inner"].Value)) : tStr.Replace("${url}", prefix + m.Groups["inner"].Value);
                //}
                tStr.Replace("${url}", prefix + m.Groups["inner"].Value);
                tStr = tStr.Replace("${inner}", m.Groups["inner"].Value);

                refText = refText.Replace(m.Groups[0].Value, tStr);
            }
        }
        //End for url with fixes

        static protected void NestedReplaceIMG(ref string refText, Regex regexMatch, string strReplace, string[] variables, string[] varDefaults)
        {
            Match m = regexMatch.Match(refText);
            while (m.Success)
            {
                string tStr = strReplace;
                int i = 0;

                foreach (string tVar in variables)
                {
                    string tValue = m.Groups[tVar].Value;
                    if (tValue.Length == 0)
                    {
                        // use default instead
                        tValue = varDefaults[i];
                    }
                    tStr = tStr.Replace("${" + tVar + "}", tValue);
                    i++;
                }
                tStr = tStr.Replace("${url}", m.Groups["inner"].Value);
                try
                {
                    UriBuilder ub = new UriBuilder(m.Groups["inner"].Value);
                    tStr = tStr.Replace("${inner}", Path.GetFileName(ub.Path));
                }
                catch (Exception)
                {
                    tStr = tStr.Replace("${inner}", "фото");
                }
                refText = refText.Substring(0, m.Groups[0].Index) + tStr +
                          refText.Substring(m.Groups[0].Index + m.Groups[0].Length);
                m = regexMatch.Match(refText);
            }
        }

        static protected void NestedReplace(ref string refText, Regex regexMatch, string strReplace, string[] variables)
        {
            Match m = regexMatch.Match(refText);
            while (m.Success)
            {
                string tStr = strReplace;

                foreach (string tVar in variables)
                {
                    tStr = tStr.Replace("${" + tVar + "}", m.Groups[tVar].Value);
                }

                tStr = tStr.Replace("${inner}", m.Groups["inner"].Value);

                refText = refText.Substring(0, m.Groups[0].Index) + tStr + refText.Substring(m.Groups[0].Index + m.Groups[0].Length);
                m = regexMatch.Match(refText);
            }
        }

        static protected int NestedReplace(ref string refText, Regex regexMatch, string strReplace, string[] variables, int count)
        {
            Match m = regexMatch.Match(refText);
            int i = 0;
            while ((m.Success) && (i < count))
            {
                string tStr = strReplace;

                foreach (string tVar in variables)
                {
                    tStr = tStr.Replace("${" + tVar + "}", m.Groups[tVar].Value);
                }

                tStr = tStr.Replace("${inner}", m.Groups["inner"].Value);

                refText = refText.Substring(0, m.Groups[0].Index) + tStr + refText.Substring(m.Groups[0].Index + m.Groups[0].Length);
                m = regexMatch.Match(refText);
                i++;
            }
            return i;
        }

        static protected void NestedReplace(ref string refText, Regex regexMatch, string strReplace)
        {
            Match m = regexMatch.Match(refText);
            while (m.Success)
            {
                string tStr = strReplace.Replace("${inner}", m.Groups["inner"].Value);
                refText = refText.Substring(0, m.Groups[0].Index) + tStr + refText.Substring(m.Groups[0].Index + m.Groups[0].Length);
                m = regexMatch.Match(refText);
            }
        }

        static protected int NestedReplace(ref string refText, Regex regexMatch, string strReplace, int count)
        {
            Match m = regexMatch.Match(refText);
            int i = 0;
            while ((m.Success) && (i < count))
            {
                string tStr = strReplace.Replace("${inner}", m.Groups["inner"].Value);
                refText = refText.Substring(0, m.Groups[0].Index) + tStr + refText.Substring(m.Groups[0].Index + m.Groups[0].Length);
                m = regexMatch.Match(refText, m.Index + strReplace.Length - m.Groups[0].ToString().Length);
                i++;
            }
            return i;
        }

        public static string Translit(string s)
        {
            string res = "";
            bool tr = true;
            for (int i = 0; i < s.Length; i++)
            {
                bool parse = false;
                if ((i < s.Length - 3) && (s[i] == '[') && (s[i + 1] == 't') && (s[i + 2] == 'r') && (s[i + 3] == ']')) { tr = false; parse = true; i += 3; }
                if ((!parse) && (i < s.Length - 4) && (s[i] == '[') && (s[i + 1] == '/') && (s[i + 2] == 't') && (s[i + 3] == 'r') && (s[i + 4] == ']')) { tr = true; parse = true; i += 4; }
                if (tr)
                {
                    if ((!parse) && (i < s.Length - 2) && (s[i] == 's') && (s[i + 1] == 'c') && (s[i + 2] == 'h')) { res += 'щ'; parse = true; i += 2; }
                    if ((!parse) && (i < s.Length - 1) && (s[i] == 'z') && (s[i + 1] == 'h')) { res += 'ж'; parse = true; i++; }
                    if ((!parse) && (i < s.Length - 1) && (s[i] == 'k') && (s[i + 1] == 'h')) { res += 'х'; parse = true; i++; }
                    if ((!parse) && (i < s.Length - 1) && (s[i] == 't') && (s[i + 1] == 'z')) { res += 'ц'; parse = true; i++; }
                    if ((!parse) && (i < s.Length - 1) && (s[i] == 'c') && (s[i + 1] == 'h')) { res += 'ч'; parse = true; i++; }
                    if ((!parse) && (i < s.Length - 1) && (s[i] == 's') && (s[i + 1] == 'h')) { res += 'ш'; parse = true; i++; }
                    if ((!parse) && (i < s.Length - 1) && (s[i] == 'y') && (s[i + 1] == 'i')) { res += 'ы'; parse = true; i++; }
                    if ((!parse) && (i < s.Length - 1) && (s[i] == 'y') && (s[i + 1] == 'e')) { res += 'э'; parse = true; i++; }
                    if ((!parse) && (i < s.Length - 1) && (s[i] == 'y') && (s[i + 1] == 'u')) { res += 'ю'; parse = true; i++; }
                    if ((!parse) && (i < s.Length - 1) && (s[i] == 'y') && (s[i + 1] == 'a')) { res += 'я'; parse = true; i++; }
                    if ((!parse) && (s[i] == 'a')) { res += 'а'; parse = true; }
                    if ((!parse) && (s[i] == 'b')) { res += 'б'; parse = true; }
                    if ((!parse) && (s[i] == 'c')) { res += 'с'; parse = true; }
                    if ((!parse) && (s[i] == 'v')) { res += 'в'; parse = true; }
                    if ((!parse) && (s[i] == 'g')) { res += 'г'; parse = true; }
                    if ((!parse) && (s[i] == 'd')) { res += 'д'; parse = true; }
                    if ((!parse) && (s[i] == 'e')) { res += 'е'; parse = true; }
                    if ((!parse) && (s[i] == 'j')) { res += 'ж'; parse = true; }
                    if ((!parse) && (s[i] == 'z')) { res += 'з'; parse = true; }
                    if ((!parse) && (s[i] == 'i')) { res += 'и'; parse = true; }
                    if ((!parse) && (s[i] == 'y')) { res += 'й'; parse = true; }
                    if ((!parse) && (s[i] == 'k')) { res += 'к'; parse = true; }
                    if ((!parse) && (s[i] == 'l')) { res += 'л'; parse = true; }
                    if ((!parse) && (s[i] == 'm')) { res += 'м'; parse = true; }
                    if ((!parse) && (s[i] == 'n')) { res += 'н'; parse = true; }
                    if ((!parse) && (s[i] == 'o')) { res += 'о'; parse = true; }
                    if ((!parse) && (s[i] == 'p')) { res += 'п'; parse = true; }
                    if ((!parse) && (s[i] == 'r')) { res += 'р'; parse = true; }
                    if ((!parse) && (s[i] == 's')) { res += 'с'; parse = true; }
                    if ((!parse) && (s[i] == 't')) { res += 'т'; parse = true; }
                    if ((!parse) && (s[i] == 'u')) { res += 'у'; parse = true; }
                    if ((!parse) && (s[i] == 'f')) { res += 'ф'; parse = true; }
                    if ((!parse) && (s[i] == 'h')) { res += 'х'; parse = true; }
                    if ((!parse) && (s[i] == 'w')) { res += 'в'; parse = true; }
                    if ((!parse) && (s[i] == '\'')) { res += 'ь'; parse = true; }

                    if ((!parse) && (i < s.Length - 2) && (s[i] == 'S') && (s[i + 1] == 'C') && (s[i + 2] == 'H')) { res += 'Щ'; parse = true; i += 2; }
                    if ((!parse) && (i < s.Length - 1) && (s[i] == 'Z') && (s[i + 1] == 'H')) { res += 'Ж'; parse = true; i++; }
                    if ((!parse) && (i < s.Length - 1) && (s[i] == 'K') && (s[i + 1] == 'H')) { res += 'Х'; parse = true; i++; }
                    if ((!parse) && (i < s.Length - 1) && (s[i] == 'T') && (s[i + 1] == 'Z')) { res += 'Ц'; parse = true; i++; }
                    if ((!parse) && (i < s.Length - 1) && (s[i] == 'C') && (s[i + 1] == 'H')) { res += 'Ч'; parse = true; i++; }
                    if ((!parse) && (i < s.Length - 1) && (s[i] == 'S') && (s[i + 1] == 'H')) { res += 'Ш'; parse = true; i++; }
                    if ((!parse) && (i < s.Length - 1) && (s[i] == 'Y') && (s[i + 1] == 'I')) { res += 'Ы'; parse = true; i++; }
                    if ((!parse) && (i < s.Length - 1) && (s[i] == 'Y') && (s[i + 1] == 'E')) { res += 'Э'; parse = true; i++; }
                    if ((!parse) && (i < s.Length - 1) && (s[i] == 'Y') && (s[i + 1] == 'U')) { res += 'Ю'; parse = true; i++; }
                    if ((!parse) && (i < s.Length - 1) && (s[i] == 'Y') && (s[i + 1] == 'A')) { res += 'Я'; parse = true; i++; }
                    if ((!parse) && (s[i] == 'A')) { res += 'А'; parse = true; }
                    if ((!parse) && (s[i] == 'B')) { res += 'Б'; parse = true; }
                    if ((!parse) && (s[i] == 'V')) { res += 'В'; parse = true; }
                    if ((!parse) && (s[i] == 'G')) { res += 'Г'; parse = true; }
                    if ((!parse) && (s[i] == 'D')) { res += 'Д'; parse = true; }
                    if ((!parse) && (s[i] == 'E')) { res += 'Е'; parse = true; }
                    if ((!parse) && (s[i] == 'J')) { res += 'Ж'; parse = true; }
                    if ((!parse) && (s[i] == 'Z')) { res += 'З'; parse = true; }
                    if ((!parse) && (s[i] == 'I')) { res += 'И'; parse = true; }
                    if ((!parse) && (s[i] == 'Y')) { res += 'Й'; parse = true; }
                    if ((!parse) && (s[i] == 'K')) { res += 'К'; parse = true; }
                    if ((!parse) && (s[i] == 'L')) { res += 'Л'; parse = true; }
                    if ((!parse) && (s[i] == 'M')) { res += 'М'; parse = true; }
                    if ((!parse) && (s[i] == 'N')) { res += 'Н'; parse = true; }
                    if ((!parse) && (s[i] == 'O')) { res += 'О'; parse = true; }
                    if ((!parse) && (s[i] == 'C')) { res += 'С'; parse = true; }
                    if ((!parse) && (s[i] == 'P')) { res += 'П'; parse = true; }
                    if ((!parse) && (s[i] == 'R')) { res += 'Р'; parse = true; }
                    if ((!parse) && (s[i] == 'S')) { res += 'С'; parse = true; }
                    if ((!parse) && (s[i] == 'T')) { res += 'Т'; parse = true; }
                    if ((!parse) && (s[i] == 'U')) { res += 'У'; parse = true; }
                    if ((!parse) && (s[i] == 'F')) { res += 'Ф'; parse = true; }
                    if ((!parse) && (s[i] == 'H')) { res += 'Х'; parse = true; }
                    if ((!parse) && (s[i] == 'W')) { res += 'В'; parse = true; }
                    if ((!parse) && (s[i] == '\'')) { res += 'Ь'; parse = true; }
                }
                if (!parse) res += s[i];
            }
            return res;
        }

        public static string Ruslangdecode(string s)
        {
            string res = "";
            for (int i = 0; i < s.Length; i++)
            {
                bool parse = false;
                if ((i < s.Length - 5) && (s[i] == '%') && (s[i + 1] == 'D') && (s[i + 2] == '0') && (s[i + 3] == '%') && (s[i + 4] == '9') && (s[i + 5] == '0')) { res += 'А'; parse = true; i += 5; }
                if ((!parse) && (i < s.Length - 5) && (s[i] == '%') && (s[i + 1] == 'D') && (s[i + 2] == '0') && (s[i + 3] == '%') && (s[i + 4] == '9') && (s[i + 5] == '1')) { res += 'Б'; parse = true; i += 5; }
                if ((!parse) && (i < s.Length - 5) && (s[i] == '%') && (s[i + 1] == 'D') && (s[i + 2] == '0') && (s[i + 3] == '%') && (s[i + 4] == '9') && (s[i + 5] == '2')) { res += 'В'; parse = true; i += 5; }
                if ((!parse) && (i < s.Length - 5) && (s[i] == '%') && (s[i + 1] == 'D') && (s[i + 2] == '0') && (s[i + 3] == '%') && (s[i + 4] == '9') && (s[i + 5] == '3')) { res += 'Г'; parse = true; i += 5; }
                if ((!parse) && (i < s.Length - 5) && (s[i] == '%') && (s[i + 1] == 'D') && (s[i + 2] == '0') && (s[i + 3] == '%') && (s[i + 4] == '9') && (s[i + 5] == '4')) { res += 'Д'; parse = true; i += 5; }
                if ((!parse) && (i < s.Length - 5) && (s[i] == '%') && (s[i + 1] == 'D') && (s[i + 2] == '0') && (s[i + 3] == '%') && (s[i + 4] == '9') && (s[i + 5] == '5')) { res += 'Е'; parse = true; i += 5; }
                if ((!parse) && (i < s.Length - 5) && (s[i] == '%') && (s[i + 1] == 'D') && (s[i + 2] == '0') && (s[i + 3] == '%') && (s[i + 4] == '9') && (s[i + 5] == '6')) { res += 'Ж'; parse = true; i += 5; }
                if ((!parse) && (i < s.Length - 5) && (s[i] == '%') && (s[i + 1] == 'D') && (s[i + 2] == '0') && (s[i + 3] == '%') && (s[i + 4] == '9') && (s[i + 5] == '7')) { res += 'З'; parse = true; i += 5; }
                if ((!parse) && (i < s.Length - 5) && (s[i] == '%') && (s[i + 1] == 'D') && (s[i + 2] == '0') && (s[i + 3] == '%') && (s[i + 4] == '9') && (s[i + 5] == '8')) { res += 'И'; parse = true; i += 5; }
                if ((!parse) && (i < s.Length - 5) && (s[i] == '%') && (s[i + 1] == 'D') && (s[i + 2] == '0') && (s[i + 3] == '%') && (s[i + 4] == '9') && (s[i + 5] == '9')) { res += 'Й'; parse = true; i += 5; }
                if ((!parse) && (i < s.Length - 5) && (s[i] == '%') && (s[i + 1] == 'D') && (s[i + 2] == '0') && (s[i + 3] == '%') && (s[i + 4] == '9') && (s[i + 5] == 'A')) { res += 'К'; parse = true; i += 5; }
                if ((!parse) && (i < s.Length - 5) && (s[i] == '%') && (s[i + 1] == 'D') && (s[i + 2] == '0') && (s[i + 3] == '%') && (s[i + 4] == '9') && (s[i + 5] == 'B')) { res += 'Л'; parse = true; i += 5; }
                if ((!parse) && (i < s.Length - 5) && (s[i] == '%') && (s[i + 1] == 'D') && (s[i + 2] == '0') && (s[i + 3] == '%') && (s[i + 4] == '9') && (s[i + 5] == 'C')) { res += 'М'; parse = true; i += 5; }
                if ((!parse) && (i < s.Length - 5) && (s[i] == '%') && (s[i + 1] == 'D') && (s[i + 2] == '0') && (s[i + 3] == '%') && (s[i + 4] == '9') && (s[i + 5] == 'D')) { res += 'Н'; parse = true; i += 5; }
                if ((!parse) && (i < s.Length - 5) && (s[i] == '%') && (s[i + 1] == 'D') && (s[i + 2] == '0') && (s[i + 3] == '%') && (s[i + 4] == '9') && (s[i + 5] == 'E')) { res += 'О'; parse = true; i += 5; }
                if ((!parse) && (i < s.Length - 5) && (s[i] == '%') && (s[i + 1] == 'D') && (s[i + 2] == '0') && (s[i + 3] == '%') && (s[i + 4] == '9') && (s[i + 5] == 'F')) { res += 'П'; parse = true; i += 5; }
                if ((!parse) && (i < s.Length - 5) && (s[i] == '%') && (s[i + 1] == 'D') && (s[i + 2] == '0') && (s[i + 3] == '%') && (s[i + 4] == 'A') && (s[i + 5] == '0')) { res += 'Р'; parse = true; i += 5; }
                if ((!parse) && (i < s.Length - 5) && (s[i] == '%') && (s[i + 1] == 'D') && (s[i + 2] == '0') && (s[i + 3] == '%') && (s[i + 4] == 'A') && (s[i + 5] == '1')) { res += 'С'; parse = true; i += 5; }
                if ((!parse) && (i < s.Length - 5) && (s[i] == '%') && (s[i + 1] == 'D') && (s[i + 2] == '0') && (s[i + 3] == '%') && (s[i + 4] == 'A') && (s[i + 5] == '2')) { res += 'Т'; parse = true; i += 5; }
                if ((!parse) && (i < s.Length - 5) && (s[i] == '%') && (s[i + 1] == 'D') && (s[i + 2] == '0') && (s[i + 3] == '%') && (s[i + 4] == 'A') && (s[i + 5] == '3')) { res += 'У'; parse = true; i += 5; }
                if ((!parse) && (i < s.Length - 5) && (s[i] == '%') && (s[i + 1] == 'D') && (s[i + 2] == '0') && (s[i + 3] == '%') && (s[i + 4] == 'A') && (s[i + 5] == '4')) { res += 'Ф'; parse = true; i += 5; }
                if ((!parse) && (i < s.Length - 5) && (s[i] == '%') && (s[i + 1] == 'D') && (s[i + 2] == '0') && (s[i + 3] == '%') && (s[i + 4] == 'A') && (s[i + 5] == '5')) { res += 'Х'; parse = true; i += 5; }
                if ((!parse) && (i < s.Length - 5) && (s[i] == '%') && (s[i + 1] == 'D') && (s[i + 2] == '0') && (s[i + 3] == '%') && (s[i + 4] == 'A') && (s[i + 5] == '6')) { res += 'Ц'; parse = true; i += 5; }
                if ((!parse) && (i < s.Length - 5) && (s[i] == '%') && (s[i + 1] == 'D') && (s[i + 2] == '0') && (s[i + 3] == '%') && (s[i + 4] == 'A') && (s[i + 5] == '7')) { res += 'Ч'; parse = true; i += 5; }
                if ((!parse) && (i < s.Length - 5) && (s[i] == '%') && (s[i + 1] == 'D') && (s[i + 2] == '0') && (s[i + 3] == '%') && (s[i + 4] == 'A') && (s[i + 5] == '8')) { res += 'Ш'; parse = true; i += 5; }
                if ((!parse) && (i < s.Length - 5) && (s[i] == '%') && (s[i + 1] == 'D') && (s[i + 2] == '0') && (s[i + 3] == '%') && (s[i + 4] == 'A') && (s[i + 5] == '9')) { res += 'Щ'; parse = true; i += 5; }
                if ((!parse) && (i < s.Length - 5) && (s[i] == '%') && (s[i + 1] == 'D') && (s[i + 2] == '0') && (s[i + 3] == '%') && (s[i + 4] == 'A') && (s[i + 5] == 'A')) { res += 'Ъ'; parse = true; i += 5; }
                if ((!parse) && (i < s.Length - 5) && (s[i] == '%') && (s[i + 1] == 'D') && (s[i + 2] == '0') && (s[i + 3] == '%') && (s[i + 4] == 'A') && (s[i + 5] == 'B')) { res += 'Ы'; parse = true; i += 5; }
                if ((!parse) && (i < s.Length - 5) && (s[i] == '%') && (s[i + 1] == 'D') && (s[i + 2] == '0') && (s[i + 3] == '%') && (s[i + 4] == 'A') && (s[i + 5] == 'C')) { res += 'Ь'; parse = true; i += 5; }
                if ((!parse) && (i < s.Length - 5) && (s[i] == '%') && (s[i + 1] == 'D') && (s[i + 2] == '0') && (s[i + 3] == '%') && (s[i + 4] == 'A') && (s[i + 5] == 'D')) { res += 'Э'; parse = true; i += 5; }
                if ((!parse) && (i < s.Length - 5) && (s[i] == '%') && (s[i + 1] == 'D') && (s[i + 2] == '0') && (s[i + 3] == '%') && (s[i + 4] == 'A') && (s[i + 5] == 'E')) { res += 'Ю'; parse = true; i += 5; }
                if ((!parse) && (i < s.Length - 5) && (s[i] == '%') && (s[i + 1] == 'D') && (s[i + 2] == '0') && (s[i + 3] == '%') && (s[i + 4] == 'A') && (s[i + 5] == 'F')) { res += 'Я'; parse = true; i += 5; }

                if ((!parse) && (i < s.Length - 5) && (s[i] == '%') && (s[i + 1] == 'D') && (s[i + 2] == '0') && (s[i + 3] == '%') && (s[i + 4] == 'B') && (s[i + 5] == '0')) { res += 'а'; parse = true; i += 5; }
                if ((!parse) && (i < s.Length - 5) && (s[i] == '%') && (s[i + 1] == 'D') && (s[i + 2] == '0') && (s[i + 3] == '%') && (s[i + 4] == 'B') && (s[i + 5] == '1')) { res += 'б'; parse = true; i += 5; }
                if ((!parse) && (i < s.Length - 5) && (s[i] == '%') && (s[i + 1] == 'D') && (s[i + 2] == '0') && (s[i + 3] == '%') && (s[i + 4] == 'B') && (s[i + 5] == '2')) { res += 'в'; parse = true; i += 5; }
                if ((!parse) && (i < s.Length - 5) && (s[i] == '%') && (s[i + 1] == 'D') && (s[i + 2] == '0') && (s[i + 3] == '%') && (s[i + 4] == 'B') && (s[i + 5] == '3')) { res += 'г'; parse = true; i += 5; }
                if ((!parse) && (i < s.Length - 5) && (s[i] == '%') && (s[i + 1] == 'D') && (s[i + 2] == '0') && (s[i + 3] == '%') && (s[i + 4] == 'B') && (s[i + 5] == '4')) { res += 'д'; parse = true; i += 5; }
                if ((!parse) && (i < s.Length - 5) && (s[i] == '%') && (s[i + 1] == 'D') && (s[i + 2] == '0') && (s[i + 3] == '%') && (s[i + 4] == 'B') && (s[i + 5] == '5')) { res += 'е'; parse = true; i += 5; }
                if ((!parse) && (i < s.Length - 5) && (s[i] == '%') && (s[i + 1] == 'D') && (s[i + 2] == '0') && (s[i + 3] == '%') && (s[i + 4] == 'B') && (s[i + 5] == '6')) { res += 'ж'; parse = true; i += 5; }
                if ((!parse) && (i < s.Length - 5) && (s[i] == '%') && (s[i + 1] == 'D') && (s[i + 2] == '0') && (s[i + 3] == '%') && (s[i + 4] == 'B') && (s[i + 5] == '7')) { res += 'з'; parse = true; i += 5; }
                if ((!parse) && (i < s.Length - 5) && (s[i] == '%') && (s[i + 1] == 'D') && (s[i + 2] == '0') && (s[i + 3] == '%') && (s[i + 4] == 'B') && (s[i + 5] == '8')) { res += 'и'; parse = true; i += 5; }
                if ((!parse) && (i < s.Length - 5) && (s[i] == '%') && (s[i + 1] == 'D') && (s[i + 2] == '0') && (s[i + 3] == '%') && (s[i + 4] == 'B') && (s[i + 5] == '9')) { res += 'й'; parse = true; i += 5; }
                if ((!parse) && (i < s.Length - 5) && (s[i] == '%') && (s[i + 1] == 'D') && (s[i + 2] == '0') && (s[i + 3] == '%') && (s[i + 4] == 'B') && (s[i + 5] == 'A')) { res += 'к'; parse = true; i += 5; }
                if ((!parse) && (i < s.Length - 5) && (s[i] == '%') && (s[i + 1] == 'D') && (s[i + 2] == '0') && (s[i + 3] == '%') && (s[i + 4] == 'B') && (s[i + 5] == 'B')) { res += 'л'; parse = true; i += 5; }
                if ((!parse) && (i < s.Length - 5) && (s[i] == '%') && (s[i + 1] == 'D') && (s[i + 2] == '0') && (s[i + 3] == '%') && (s[i + 4] == 'B') && (s[i + 5] == 'C')) { res += 'м'; parse = true; i += 5; }
                if ((!parse) && (i < s.Length - 5) && (s[i] == '%') && (s[i + 1] == 'D') && (s[i + 2] == '0') && (s[i + 3] == '%') && (s[i + 4] == 'B') && (s[i + 5] == 'D')) { res += 'н'; parse = true; i += 5; }
                if ((!parse) && (i < s.Length - 5) && (s[i] == '%') && (s[i + 1] == 'D') && (s[i + 2] == '0') && (s[i + 3] == '%') && (s[i + 4] == 'B') && (s[i + 5] == 'E')) { res += 'о'; parse = true; i += 5; }
                if ((!parse) && (i < s.Length - 5) && (s[i] == '%') && (s[i + 1] == 'D') && (s[i + 2] == '0') && (s[i + 3] == '%') && (s[i + 4] == 'B') && (s[i + 5] == 'F')) { res += 'п'; parse = true; i += 5; }
                if ((!parse) && (i < s.Length - 5) && (s[i] == '%') && (s[i + 1] == 'D') && (s[i + 2] == '0') && (s[i + 3] == '%') && (s[i + 4] == 'C') && (s[i + 5] == '0')) { res += 'р'; parse = true; i += 5; }
                if ((!parse) && (i < s.Length - 5) && (s[i] == '%') && (s[i + 1] == 'D') && (s[i + 2] == '0') && (s[i + 3] == '%') && (s[i + 4] == 'C') && (s[i + 5] == '1')) { res += 'с'; parse = true; i += 5; }
                if ((!parse) && (i < s.Length - 5) && (s[i] == '%') && (s[i + 1] == 'D') && (s[i + 2] == '0') && (s[i + 3] == '%') && (s[i + 4] == 'C') && (s[i + 5] == '2')) { res += 'т'; parse = true; i += 5; }
                if ((!parse) && (i < s.Length - 5) && (s[i] == '%') && (s[i + 1] == 'D') && (s[i + 2] == '0') && (s[i + 3] == '%') && (s[i + 4] == 'C') && (s[i + 5] == '3')) { res += 'у'; parse = true; i += 5; }
                if ((!parse) && (i < s.Length - 5) && (s[i] == '%') && (s[i + 1] == 'D') && (s[i + 2] == '0') && (s[i + 3] == '%') && (s[i + 4] == 'C') && (s[i + 5] == '4')) { res += 'ф'; parse = true; i += 5; }
                if ((!parse) && (i < s.Length - 5) && (s[i] == '%') && (s[i + 1] == 'D') && (s[i + 2] == '0') && (s[i + 3] == '%') && (s[i + 4] == 'C') && (s[i + 5] == '5')) { res += 'х'; parse = true; i += 5; }
                if ((!parse) && (i < s.Length - 5) && (s[i] == '%') && (s[i + 1] == 'D') && (s[i + 2] == '0') && (s[i + 3] == '%') && (s[i + 4] == 'C') && (s[i + 5] == '6')) { res += 'ц'; parse = true; i += 5; }
                if ((!parse) && (i < s.Length - 5) && (s[i] == '%') && (s[i + 1] == 'D') && (s[i + 2] == '0') && (s[i + 3] == '%') && (s[i + 4] == 'C') && (s[i + 5] == '7')) { res += 'ч'; parse = true; i += 5; }
                if ((!parse) && (i < s.Length - 5) && (s[i] == '%') && (s[i + 1] == 'D') && (s[i + 2] == '0') && (s[i + 3] == '%') && (s[i + 4] == 'C') && (s[i + 5] == '8')) { res += 'ш'; parse = true; i += 5; }
                if ((!parse) && (i < s.Length - 5) && (s[i] == '%') && (s[i + 1] == 'D') && (s[i + 2] == '0') && (s[i + 3] == '%') && (s[i + 4] == 'C') && (s[i + 5] == '9')) { res += 'щ'; parse = true; i += 5; }
                if ((!parse) && (i < s.Length - 5) && (s[i] == '%') && (s[i + 1] == 'D') && (s[i + 2] == '0') && (s[i + 3] == '%') && (s[i + 4] == 'C') && (s[i + 5] == 'A')) { res += 'ъ'; parse = true; i += 5; }
                if ((!parse) && (i < s.Length - 5) && (s[i] == '%') && (s[i + 1] == 'D') && (s[i + 2] == '0') && (s[i + 3] == '%') && (s[i + 4] == 'C') && (s[i + 5] == 'B')) { res += 'ы'; parse = true; i += 5; }
                if ((!parse) && (i < s.Length - 5) && (s[i] == '%') && (s[i + 1] == 'D') && (s[i + 2] == '0') && (s[i + 3] == '%') && (s[i + 4] == 'C') && (s[i + 5] == 'C')) { res += 'ь'; parse = true; i += 5; }
                if ((!parse) && (i < s.Length - 5) && (s[i] == '%') && (s[i + 1] == 'D') && (s[i + 2] == '0') && (s[i + 3] == '%') && (s[i + 4] == 'C') && (s[i + 5] == 'D')) { res += 'э'; parse = true; i += 5; }
                if ((!parse) && (i < s.Length - 5) && (s[i] == '%') && (s[i + 1] == 'D') && (s[i + 2] == '0') && (s[i + 3] == '%') && (s[i + 4] == 'C') && (s[i + 5] == 'E')) { res += 'ю'; parse = true; i += 5; }
                if ((!parse) && (i < s.Length - 5) && (s[i] == '%') && (s[i + 1] == 'D') && (s[i + 2] == '0') && (s[i + 3] == '%') && (s[i + 4] == 'C') && (s[i + 5] == 'F')) { res += 'я'; parse = true; i += 5; }


                if (!parse) res += s[i];
            }
            return res;
        }

        public static string Ruslangencode(string s)
        {
            string res = "";
            for (int i = 0; i < s.Length; i++)
            {
                bool parse = false;
                if ((s[i] == 'А')) { res += "%D0%90"; parse = true; i++; }
                if ((!parse) && (s[i] == 'Б')) { res += "%D0%91"; parse = true; i++; }
                if ((!parse) && (s[i] == 'В')) { res += "%D0%92"; parse = true; i++; }
                if ((!parse) && (s[i] == 'Г')) { res += "%D0%93"; parse = true; i++; }
                if ((!parse) && (s[i] == 'Д')) { res += "%D0%94"; parse = true; i++; }
                if ((!parse) && (s[i] == 'Е')) { res += "%D0%95"; parse = true; i++; }
                if ((!parse) && (s[i] == 'Ж')) { res += "%D0%96"; parse = true; i++; }
                if ((!parse) && (s[i] == 'З')) { res += "%D0%97"; parse = true; i++; }
                if ((!parse) && (s[i] == 'И')) { res += "%D0%98"; parse = true; i++; }
                if ((!parse) && (s[i] == 'Й')) { res += "%D0%99"; parse = true; i++; }
                if ((!parse) && (s[i] == 'К')) { res += "%D0%9A"; parse = true; i++; }
                if ((!parse) && (s[i] == 'Л')) { res += "%D0%9B"; parse = true; i++; }
                if ((!parse) && (s[i] == 'М')) { res += "%D0%9C"; parse = true; i++; }
                if ((!parse) && (s[i] == 'Н')) { res += "%D0%9D"; parse = true; i++; }
                if ((!parse) && (s[i] == 'О')) { res += "%D0%9E"; parse = true; i++; }
                if ((!parse) && (s[i] == 'П')) { res += "%D0%9F"; parse = true; i++; }
                if ((!parse) && (s[i] == 'Р')) { res += "%D0%A0"; parse = true; i++; }
                if ((!parse) && (s[i] == 'С')) { res += "%D0%A1"; parse = true; i++; }
                if ((!parse) && (s[i] == 'Т')) { res += "%D0%A2"; parse = true; i++; }
                if ((!parse) && (s[i] == 'У')) { res += "%D0%A3"; parse = true; i++; }
                if ((!parse) && (s[i] == 'Ф')) { res += "%D0%A4"; parse = true; i++; }
                if ((!parse) && (s[i] == 'Х')) { res += "%D0%A5"; parse = true; i++; }
                if ((!parse) && (s[i] == 'Ц')) { res += "%D0%A6"; parse = true; i++; }
                if ((!parse) && (s[i] == 'Ч')) { res += "%D0%A7"; parse = true; i++; }
                if ((!parse) && (s[i] == 'Ш')) { res += "%D0%A8"; parse = true; i++; }
                if ((!parse) && (s[i] == 'Щ')) { res += "%D0%A9"; parse = true; i++; }
                if ((!parse) && (s[i] == 'Ъ')) { res += "%D0%AA"; parse = true; i++; }
                if ((!parse) && (s[i] == 'Ы')) { res += "%D0%AB"; parse = true; i++; }
                if ((!parse) && (s[i] == 'Ь')) { res += "%D0%AC"; parse = true; i++; }
                if ((!parse) && (s[i] == 'Э')) { res += "%D0%AD"; parse = true; i++; }
                if ((!parse) && (s[i] == 'Ю')) { res += "%D0%AE"; parse = true; i++; }
                if ((!parse) && (s[i] == 'Я')) { res += "%D0%AF"; parse = true; i++; }


                if ((!parse) && (s[i] == 'а')) { res += "%D0%B0"; parse = true; i++; }
                if ((!parse) && (s[i] == 'б')) { res += "%D0%B1"; parse = true; i++; }
                if ((!parse) && (s[i] == 'в')) { res += "%D0%B2"; parse = true; i++; }
                if ((!parse) && (s[i] == 'г')) { res += "%D0%B3"; parse = true; i++; }
                if ((!parse) && (s[i] == 'д')) { res += "%D0%B4"; parse = true; i++; }
                if ((!parse) && (s[i] == 'е')) { res += "%D0%B5"; parse = true; i++; }
                if ((!parse) && (s[i] == 'ж')) { res += "%D0%B6"; parse = true; i++; }
                if ((!parse) && (s[i] == 'з')) { res += "%D0%B7"; parse = true; i++; }
                if ((!parse) && (s[i] == 'и')) { res += "%D0%B8"; parse = true; i++; }
                if ((!parse) && (s[i] == 'й')) { res += "%D0%B9"; parse = true; i++; }
                if ((!parse) && (s[i] == 'к')) { res += "%D0%BA"; parse = true; i++; }
                if ((!parse) && (s[i] == 'л')) { res += "%D0%BB"; parse = true; i++; }
                if ((!parse) && (s[i] == 'м')) { res += "%D0%BC"; parse = true; i++; }
                if ((!parse) && (s[i] == 'н')) { res += "%D0%BD"; parse = true; i++; }
                if ((!parse) && (s[i] == 'о')) { res += "%D0%BE"; parse = true; i++; }
                if ((!parse) && (s[i] == 'п')) { res += "%D0%BF"; parse = true; i++; }
                if ((!parse) && (s[i] == 'р')) { res += "%D0%C0"; parse = true; i++; }
                if ((!parse) && (s[i] == 'с')) { res += "%D0%C1"; parse = true; i++; }
                if ((!parse) && (s[i] == 'т')) { res += "%D0%C2"; parse = true; i++; }
                if ((!parse) && (s[i] == 'у')) { res += "%D0%C3"; parse = true; i++; }
                if ((!parse) && (s[i] == 'ф')) { res += "%D0%C4"; parse = true; i++; }
                if ((!parse) && (s[i] == 'х')) { res += "%D0%C5"; parse = true; i++; }
                if ((!parse) && (s[i] == 'ц')) { res += "%D0%C6"; parse = true; i++; }
                if ((!parse) && (s[i] == 'ч')) { res += "%D0%C7"; parse = true; i++; }
                if ((!parse) && (s[i] == 'ш')) { res += "%D0%C8"; parse = true; i++; }
                if ((!parse) && (s[i] == 'щ')) { res += "%D0%C9"; parse = true; i++; }
                if ((!parse) && (s[i] == 'ъ')) { res += "%D0%CA"; parse = true; i++; }
                if ((!parse) && (s[i] == 'ы')) { res += "%D0%CB"; parse = true; i++; }
                if ((!parse) && (s[i] == 'ь')) { res += "%D0%CC"; parse = true; i++; }
                if ((!parse) && (s[i] == 'э')) { res += "%D0%CD"; parse = true; i++; }
                if ((!parse) && (s[i] == 'ю')) { res += "%D0%CE"; parse = true; i++; }
                if ((!parse) && (s[i] == 'я')) { res += "%D0%CF"; parse = true; i++; }


                if (!parse) res += s[i];
            }
            return res;
        }

        public static string nbsp(string s)
        {
            s = s.Replace("  ", "&nbsp; ");
            s = s.Replace("  ", " &nbsp;");
            s = s.Replace("\t", "&nbsp; &nbsp;&nbsp;");
            s = s.Replace("\r\n", "<br />");
            s = s.Replace("[domain]", ConfigurationManager.AppSettings["domain"]);
            return s;
        }

        public static bool Findspam(string s)
        {
            s = s.Trim();
            bool spam = s.ToLower().IndexOf("[g=", StringComparison.Ordinal) >= 0;
            if (s.ToLower().IndexOf("[с=", StringComparison.Ordinal) >= 0) spam = true;
            Regex findSpam = new Regex(@"[-a-zA-Z0-9@:%_\+.~#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9@:%_\+.~#?&//=]*)?", m_options);
            if (findSpam.IsMatch(s)) spam = true;
            return spam;
        }

        public static string Searchstring(string s)
        {
            string res = "\"";
            for (int i = 0; i < s.Length; i++)
            {
                bool parse = false;
                if ((s[i].ToString(CultureInfo.InvariantCulture).ToLower() == " ")) { res += " "; parse = true; }
                if ((!parse) && (s[i].ToString(CultureInfo.InvariantCulture).ToLower() == "*")) { res += "*"; parse = true; }
                if ((!parse) && (s[i].ToString(CultureInfo.InvariantCulture).ToLower() == "(")) { res += "("; parse = true; }
                if ((!parse) && (s[i].ToString(CultureInfo.InvariantCulture).ToLower() == ")")) { res += ")"; parse = true; }
                if ((!parse) && (s[i].ToString(CultureInfo.InvariantCulture).ToLower() == "<")) { res += "<"; parse = true; }
                if ((!parse) && (s[i].ToString(CultureInfo.InvariantCulture).ToLower() == ">")) { res += ">"; parse = true; }
                if ((!parse) && (s[i].ToString(CultureInfo.InvariantCulture).ToLower() == "%")) { res += "%"; parse = true; }
                if ((!parse) && (s[i].ToString(CultureInfo.InvariantCulture).ToLower() == "@")) { res += "@"; parse = true; }
                if ((!parse) && (s[i].ToString(CultureInfo.InvariantCulture).ToLower() == "!")) { res += "!"; parse = true; }
                if ((!parse) && (s[i].ToString(CultureInfo.InvariantCulture).ToLower() == ".")) { res += "."; parse = true; }
                if ((!parse) && (s[i].ToString(CultureInfo.InvariantCulture).ToLower() == ",")) { res += ","; parse = true; }
                if ((!parse) && (s[i].ToString(CultureInfo.InvariantCulture).ToLower() == "?")) { res += "?"; parse = true; }
                if ((!parse) && (s[i].ToString(CultureInfo.InvariantCulture).ToLower() == "!")) { res += "!"; parse = true; }

                if ((!parse) && (s[i].ToString(CultureInfo.InvariantCulture).ToLower() == "a")) { res += "a"; parse = true; }
                if ((!parse) && (s[i].ToString(CultureInfo.InvariantCulture).ToLower() == "b")) { res += "b"; parse = true; }
                if ((!parse) && (s[i].ToString(CultureInfo.InvariantCulture).ToLower() == "c")) { res += "c"; parse = true; }
                if ((!parse) && (s[i].ToString(CultureInfo.InvariantCulture).ToLower() == "d")) { res += "d"; parse = true; }
                if ((!parse) && (s[i].ToString(CultureInfo.InvariantCulture).ToLower() == "e")) { res += "e"; parse = true; }
                if ((!parse) && (s[i].ToString(CultureInfo.InvariantCulture).ToLower() == "f")) { res += "f"; parse = true; }
                if ((!parse) && (s[i].ToString(CultureInfo.InvariantCulture).ToLower() == "g")) { res += "g"; parse = true; }
                if ((!parse) && (s[i].ToString(CultureInfo.InvariantCulture).ToLower() == "h")) { res += "h"; parse = true; }
                if ((!parse) && (s[i].ToString(CultureInfo.InvariantCulture).ToLower() == "i")) { res += "i"; parse = true; }
                if ((!parse) && (s[i].ToString(CultureInfo.InvariantCulture).ToLower() == "j")) { res += "j"; parse = true; }
                if ((!parse) && (s[i].ToString(CultureInfo.InvariantCulture).ToLower() == "k")) { res += "k"; parse = true; }
                if ((!parse) && (s[i].ToString(CultureInfo.InvariantCulture).ToLower() == "l")) { res += "l"; parse = true; }
                if ((!parse) && (s[i].ToString(CultureInfo.InvariantCulture).ToLower() == "m")) { res += "m"; parse = true; }
                if ((!parse) && (s[i].ToString(CultureInfo.InvariantCulture).ToLower() == "n")) { res += "n"; parse = true; }
                if ((!parse) && (s[i].ToString(CultureInfo.InvariantCulture).ToLower() == "o")) { res += "o"; parse = true; }
                if ((!parse) && (s[i].ToString(CultureInfo.InvariantCulture).ToLower() == "p")) { res += "p"; parse = true; }
                if ((!parse) && (s[i].ToString(CultureInfo.InvariantCulture).ToLower() == "q")) { res += "q"; parse = true; }
                if ((!parse) && (s[i].ToString(CultureInfo.InvariantCulture).ToLower() == "r")) { res += "r"; parse = true; }
                if ((!parse) && (s[i].ToString(CultureInfo.InvariantCulture).ToLower() == "s")) { res += "s"; parse = true; }
                if ((!parse) && (s[i].ToString(CultureInfo.InvariantCulture).ToLower() == "t")) { res += "t"; parse = true; }
                if ((!parse) && (s[i].ToString(CultureInfo.InvariantCulture).ToLower() == "u")) { res += "u"; parse = true; }
                if ((!parse) && (s[i].ToString(CultureInfo.InvariantCulture).ToLower() == "v")) { res += "v"; parse = true; }
                if ((!parse) && (s[i].ToString(CultureInfo.InvariantCulture).ToLower() == "w")) { res += "w"; parse = true; }
                if ((!parse) && (s[i].ToString(CultureInfo.InvariantCulture).ToLower() == "x")) { res += "x"; parse = true; }
                if ((!parse) && (s[i].ToString(CultureInfo.InvariantCulture).ToLower() == "y")) { res += "y"; parse = true; }
                if ((!parse) && (s[i].ToString(CultureInfo.InvariantCulture).ToLower() == "z")) { res += "z"; parse = true; }
                                               
                if ((!parse) && (s[i].ToString(CultureInfo.InvariantCulture).ToLower() == "а")) { res += "а"; parse = true; }
                if ((!parse) && (s[i].ToString(CultureInfo.InvariantCulture).ToLower() == "б")) { res += "б"; parse = true; }
                if ((!parse) && (s[i].ToString(CultureInfo.InvariantCulture).ToLower() == "в")) { res += "в"; parse = true; }
                if ((!parse) && (s[i].ToString(CultureInfo.InvariantCulture).ToLower() == "г")) { res += "г"; parse = true; }
                if ((!parse) && (s[i].ToString(CultureInfo.InvariantCulture).ToLower() == "д")) { res += "д"; parse = true; }
                if ((!parse) && (s[i].ToString(CultureInfo.InvariantCulture).ToLower() == "е")) { res += "е"; parse = true; }
                if ((!parse) && (s[i].ToString(CultureInfo.InvariantCulture).ToLower() == "ё")) { res += "ё"; parse = true; }
                if ((!parse) && (s[i].ToString(CultureInfo.InvariantCulture).ToLower() == "ж")) { res += "ж"; parse = true; }
                if ((!parse) && (s[i].ToString(CultureInfo.InvariantCulture).ToLower() == "з")) { res += "з"; parse = true; }
                if ((!parse) && (s[i].ToString(CultureInfo.InvariantCulture).ToLower() == "и")) { res += "и"; parse = true; }
                if ((!parse) && (s[i].ToString(CultureInfo.InvariantCulture).ToLower() == "й")) { res += "й"; parse = true; }
                if ((!parse) && (s[i].ToString(CultureInfo.InvariantCulture).ToLower() == "к")) { res += "к"; parse = true; }
                if ((!parse) && (s[i].ToString(CultureInfo.InvariantCulture).ToLower() == "л")) { res += "л"; parse = true; }
                if ((!parse) && (s[i].ToString(CultureInfo.InvariantCulture).ToLower() == "м")) { res += "м"; parse = true; }
                if ((!parse) && (s[i].ToString(CultureInfo.InvariantCulture).ToLower() == "н")) { res += "н"; parse = true; }
                if ((!parse) && (s[i].ToString(CultureInfo.InvariantCulture).ToLower() == "о")) { res += "о"; parse = true; }
                if ((!parse) && (s[i].ToString(CultureInfo.InvariantCulture).ToLower() == "п")) { res += "п"; parse = true; }
                if ((!parse) && (s[i].ToString(CultureInfo.InvariantCulture).ToLower() == "р")) { res += "р"; parse = true; }
                if ((!parse) && (s[i].ToString(CultureInfo.InvariantCulture).ToLower() == "с")) { res += "с"; parse = true; }
                if ((!parse) && (s[i].ToString(CultureInfo.InvariantCulture).ToLower() == "т")) { res += "т"; parse = true; }
                if ((!parse) && (s[i].ToString(CultureInfo.InvariantCulture).ToLower() == "у")) { res += "у"; parse = true; }
                if ((!parse) && (s[i].ToString(CultureInfo.InvariantCulture).ToLower() == "ф")) { res += "ф"; parse = true; }
                if ((!parse) && (s[i].ToString(CultureInfo.InvariantCulture).ToLower() == "х")) { res += "х"; parse = true; }
                if ((!parse) && (s[i].ToString(CultureInfo.InvariantCulture).ToLower() == "ц")) { res += "ц"; parse = true; }
                if ((!parse) && (s[i].ToString(CultureInfo.InvariantCulture).ToLower() == "ч")) { res += "ч"; parse = true; }
                if ((!parse) && (s[i].ToString(CultureInfo.InvariantCulture).ToLower() == "ш")) { res += "ш"; parse = true; }
                if ((!parse) && (s[i].ToString(CultureInfo.InvariantCulture).ToLower() == "щ")) { res += "щ"; parse = true; }
                if ((!parse) && (s[i].ToString(CultureInfo.InvariantCulture).ToLower() == "ъ")) { res += "ъ"; parse = true; }
                if ((!parse) && (s[i].ToString(CultureInfo.InvariantCulture).ToLower() == "ы")) { res += "ы"; parse = true; }
                if ((!parse) && (s[i].ToString(CultureInfo.InvariantCulture).ToLower() == "ь")) { res += "ь"; parse = true; }
                if ((!parse) && (s[i].ToString(CultureInfo.InvariantCulture).ToLower() == "э")) { res += "э"; parse = true; }
                if ((!parse) && (s[i].ToString(CultureInfo.InvariantCulture).ToLower() == "ю")) { res += "ю"; parse = true; }
                if ((!parse) && (s[i].ToString(CultureInfo.InvariantCulture).ToLower() == "я")) { res += "я"; }
            }
            res += "\"";
            return res;
        }

        public static string Bbcode(string s, bool img)
        {
            s = s.Replace("[domain]", ConfigurationManager.AppSettings["domain"]);
            if (img) NestedReplace(ref s, r_youtube, @"<object width=""425"" height=""350""><param name=""movie"" value=""http://www.youtube.com/v/${id}""></param><embed src=""http://www.youtube.com/v/${id}"" type=""application/x-shockwave-flash"" width=""425"" height=""350""></embed></object><br /><br />", new[] { "id" }, 1);
            else NestedReplaceURL1(ref s, r_youtube, "<a href=\"${url}\">${inner}</a>", new[] { "http" }, new[] { "http://" });
            NestedReplace(ref s, r_bold, "<b>${inner}</b>");
            NestedReplace(ref s, r_italic, "<i>${inner}</i>");
            NestedReplace(ref s, r_paragraph, "<p>${inner}</p>");
            NestedReplace(ref s, r_underline, "<u>${inner}</u>");
            NestedReplace(ref s, r_strike, "<s>${inner}</s>");
            NestedReplace(ref s, r_red, "<span class=\"attention\">${inner}</span>");
            NestedReplace(ref s, r_strong, "<strong>${inner}</strong>");
            NestedReplace(ref s, r_spoiler, "<span class=\"spoiler\">${inner}</span>");
            NestedReplace(ref s, r_off, "<small class=\"gr\">${inner}</small>");
            //s = r_hr.Replace(s, "<hr noshade/>");
            //NestedReplace(ref s, r_url2, "<a rel=\"nofollow\" href=\"${http}${url}\">${inner}</a>", new string[] { "url", "http" }, new string[] { "", "http://" });
            NestedReplaceURL1(ref s, r_url1, "<a href=\"${http}${inner}\">${inner}</a>", new[] { "http" }, new[] { "http://" });
            NestedReplaceURL2(ref s, r_url2, "<a href=\"${url}\">${inner}</a>", new[] { "http" }, new[] { "http://" });

            if (img) NestedReplace(ref s, r_img, "<img src=\"${http}${inner}\"/>", new[] { "http" }, new[] { "http://" });
            else NestedReplaceIMG(ref s, r_img, "<a rel=\"nofollow\" href=\"${http}${url}\">${inner}</a>", new[] { "http" }, new[] { "http://" });
            if ((img) && (s.IndexOf("<", StringComparison.Ordinal) == -1)) NestedReplace(ref s, r_input, "<input type=\"text\" value=\"${inner}\" />");
            s = s.Replace("[br]", "<br />");

            //s = r_urlAuto.Replace(s, "<a href=\"$2\">$4</a>");        

            return s;
        }

        public static string BbcodeNourl(string s, bool img)
        {
            s = s.Replace("[br]", "<br />");
            s = s.Replace("[domain]", ConfigurationManager.AppSettings["domain"]);
            NestedReplace(ref s, r_bold, "<b>${inner}</b>");
            NestedReplace(ref s, r_italic, "<i>${inner}</i>");
            NestedReplace(ref s, r_paragraph, "<p>${inner}</p>");
            NestedReplace(ref s, r_underline, "<u>${inner}</u>");
            NestedReplace(ref s, r_strike, "<s>${inner}</s>");
            //NestedReplace(ref res, r_url2, "<a rel=\"nofollow\" href=\"${http}${url}\">${inner}</a>", new string[] { "url", "http" }, new string[] { "", "http://" });
            //NestedReplace(ref res, r_url1, "<a rel=\"nofollow\" href=\"${http}${inner}\">${http}${inner}</a>", new string[] { "http" }, new string[] { "http://" });
            if (img) NestedReplace(ref s, r_img, "<img src=\"${http}${inner}\"/>", new[] { "http" }, new[] { "http://" });
            else NestedReplace(ref s, r_img, "<a rel=\"nofollow\" href=\"${http}${inner}\">${http}${inner}</a>", new[] { "http" }, new[] { "http://" });
            return s;
        }

        public static string BbcodeNoUrlImg(string s)
        {
            s = s.Replace("[br]", "<br />");
            s = s.Replace("[domain]", ConfigurationManager.AppSettings["domain"]);
            NestedReplace(ref s, r_bold, "<b>${inner}</b>");
            NestedReplace(ref s, r_italic, "<i>${inner}</i>");
            NestedReplace(ref s, r_paragraph, "<p>${inner}</p>");
            NestedReplace(ref s, r_underline, "<u>${inner}</u>");
            NestedReplace(ref s, r_strike, "<s>${inner}</s>");
            return s;
        }

        public static string smiles(string s, int max, int type)
        {
            Regex smile1 = new Regex(@"(:-[ДD])|(:[ДD])", m_options); // :-D
            Regex smile2 = new Regex(@"(;-[ДD])|(;-[ДD])", m_options); // ;-D
            Regex smile3 = new Regex(@"(:-\))|(:\))", m_options); // :-)
            Regex smile4 = new Regex(@"(:-!)|(:!)", m_options); // :-!
            Regex smile5 = new Regex(@"(:-\\)|(:\\)", m_options); // :-\\
            Regex smile6 = new Regex(@"(:-[XХ])|(:[XХ])", m_options); // :-x
            Regex smile7 = new Regex(@"(:-\|)|(:\|)", m_options); // :-|
            Regex smile8 = new Regex(@"(:-\*)|(:\*)", m_options); // :-*
            Regex smile9 = new Regex(@"(:-[I\[])|(:[I\[])", m_options); // :-[ :-I
            Regex smile10 = new Regex(@"8-\)", m_options); // 8-)
            Regex smile11 = new Regex(@"(:-[PР])|(:[PР])", m_options); // :-P
            Regex smile12 = new Regex(@"(;-\))", m_options); // ;-)
            Regex smile13 = new Regex(@"(:-\()|(:\()", m_options); // :-(
            Regex smile14 = new Regex(@"(:-\)\))|(:\)\))", m_options); // :-(
            Regex smile15 = new Regex(@"(:-\'\()|(:\'\()", m_options); // :'(
            Regex smile16 = new Regex(@"=-[OО]", m_options); // =-o
            Regex smile17 = new Regex(@"\\[mм]/", m_options); // \m/
            Regex smile18 = new Regex(@":[hх][iи]:", m_options); // :hi:
            Regex smile19 = new Regex(@":[oо][kк]:", m_options); // :ok:
            Regex smile20 = new Regex(@":[dд][kк]:", m_options); // :dk:
            Regex smile21 = new Regex(@"(:[sс][uу]:)|(:[sс][pп][uуа][cсц][eе]_[uуа][pп]:)", m_options); // :su: spuce_up
            Regex smile22 = new Regex(@"(:[fф][mм]:)|(:[fф][iи][rр][sс][tт]_[mм][oо][vв][eе]:)", m_options); // :fm: first_move

            Regex smile24 = new Regex(@":[sс][oо][sс]:", m_options); // :sos:
            Regex smile25 = new Regex(@"(:yes:)|(:ес:)", m_options); // :yes:
            Regex smile26 = new Regex(@"&[g][t];:[OО]", m_options); // >:O
            Regex smile27 = new Regex(@":[fф][eеи][aа][rр]:", m_options); // :fear:
            Regex smile28 = new Regex(@"(:[kк][iи][nн][gг]:)|(:царь:)", m_options); // :king:
            Regex smile29 = new Regex(@":[kк][iи][sс][sс]:", m_options); // :kiss:
            Regex smile30 = new Regex(@":[bб][oо][mм][bб]а?:", m_options); // :bomb:
            Regex smile31 = new Regex(@":[rр][tт][fф][mм]:", m_options); // :rtfm:
            Regex smile32 = new Regex(@"(:[lл][oо][lл]:)|(:[rр][oо][fф][lл]:)", m_options); // :lol: :rofl:
            Regex smile34 = new Regex(@":[rр][oо][sсз][eеа]:", m_options); // :rose:
            Regex smile35 = new Regex(@":[rр][eе][aа][dд]:", m_options); // :read:
            Regex smile36 = new Regex(@"(:[sс][oо][nн][gг]:)|(:песня:)", m_options); // :song:
            Regex smile37 = new Regex(@":[sс][oо][rр][yйи]:", m_options); // :sory:
            Regex smile38 = new Regex(@":[sс][tт][oо][pп]:", m_options); // :stop:
            Regex smile39 = new Regex(@":[sс][aа][rр][cск]:", m_options); // :sarc:
            Regex smile40 = new Regex(@":[sс][iи][gг][hх]:", m_options); // :sigh:
            Regex smile45 = new Regex(@":[dд][rр][iи][nн][kк]:", m_options); // :drink:
            Regex smile46 = new Regex(@":[cск][rр][aаэе][zз][yйи]:", m_options); // :crazy:
            Regex smile47 = new Regex(@"(:[hх][eе][aа][rр][tт]:)|(:сердце:)", m_options); // :heart:
            Regex smile48 = new Regex(@"(:[dд][iи][sс][tт][rр][uу][tт][hн]:)|(:недоверие:)", m_options); // :distruth:
            Regex smile49 = new Regex(@"(:[sс][mм][oо][kк][eе]:)|(:курю:)", m_options); // :smoke:
            Regex smile50 = new Regex(@":[tт][eе][aа][sс][eе]:", m_options); // :tease:
            Regex smile51 = new Regex(@":[fф][lл][iи][rр][tт]:", m_options); // :flirt:
            Regex smile52 = new Regex(@"(:[sс][lл][eе][eе][pп]:)|(:слип:)|(:сплю:)", m_options); // :sleep:
            Regex smile53 = new Regex(@"(:[tт][iи][rр][eе][dд]:)|(:устал:)", m_options); // :tired:
            Regex smile54 = new Regex(@"(:[tт][hх][iи][nн][kк]:)|(:синк:)", m_options); // :think:
            Regex smile55 = new Regex(@"(:yahoo:)|(:яхоо:)|(:яху:)", m_options); // :yahoo:
            Regex smile56 = new Regex(@"(:[rр][oо][lл][lл][eе][rр]:)|(:[rр][oо][lл][eе][rр]:)", m_options); // :roller:
            Regex smile57 = new Regex(@":[bб][oо][kк][aа][lл][yйыи]:", m_options); // :bokaly:
            Regex smile58 = new Regex(@":[hх][uуа][nн][tт][eе][rр]:", m_options); // :hunter:
            Regex smile59 = new Regex(@":[wв][iи][zз][aа][rр][dд]:", m_options); // :wizard:
            Regex smile60 = new Regex(@":[iи][mм][pп][oо][sс][sс]:", m_options); // :imposs:
            Regex smile61 = new Regex(@":[tт][eе][nн][dд][eе][rр]:", m_options); // :tender:
            Regex smile62 = new Regex(@":[dд][iи][aа][bб][lл][oо]:", m_options); // :diablo:
            Regex smile63 = new Regex(@":[iи][nн][lл][oо][vв][eе]:", m_options); // :inlove:
            Regex smile64 = new Regex(@"(:[fф][rр][iи][eе][nн][dд]:)|(:френд:)", m_options); // :friend:
            Regex smile65 = new Regex(@"(:punish:)|(:п[уa]ниш:)", m_options); // :punish:

            Regex smile67 = new Regex(@":[fф][lл][oо][wв][eе][rр][sс]:", m_options); // :flowers:
            Regex smile68 = new Regex(@":[gг]_[dд][rр][iи][nн][kк]:", m_options); // :g_drink:
            Regex smile69 = new Regex(@":[gг]_[cск][rр][aае][zз][yйи]:", m_options); // :g_crazy:
            Regex smile70 = new Regex(@":[hх][yйи][sс][tт][eе][rр][iи][cск]:", m_options); // :hysteric:
            Regex smile71 = new Regex(@":[gг][iиеё][rр][lл]_[cск][rр][yйи]:", m_options); // :girl_cry:
            Regex smile72 = new Regex(@"(:[cсц][eе][nн][sс][oо][rр][eе][dд]:)|(:цензура:)", m_options); // :censored:

            Regex smile74 = new Regex(@":[hхг][oо][sс][pп][iи][tт][aа][lл]:", m_options); // :hospital:

            Regex smile76 = new Regex(@":[gг]_[iи][nн]_[lл][oоа][vв][eе]:", m_options); // :g_in_love:
            Regex smile77 = new Regex(@":[rр][aа][bб]:", m_options); // :rab:
            Regex smile78 = new Regex(@"(:ninj:)|(:нинзя:)", m_options); // :нинзя:

            int count = 0;
            string add = "nosmiles";
            string ext = ".gif";
            if (type == 1)
            {
                add = "static/";
            }
            if (type == 2)
            {
                add = "dynamic/";
            }
            if (type == 3)
            {
                add = "awap/";
            }
            if (type == 4)
            {
                add = "wbmp/";
                ext = ".wbmp";
            }
            if (type == 5)
            {
                add = "ext/";
            }
            //s = s.Replace("&quot;", "&quot; ");
            //s = s.Replace("&nbsp;", "&nbsp; ");
            //s = s.Replace("&gt;", "&gt; ");
            //s = s.Replace("&lt;", "&lt; ");
            //string smilesDir = "<img src=\"" + HttpContext.Current.Response.ApplyAppPathModifier(domain + "/Content/smiles/") + add;
            string smilesDir = "<img src=\"/Content/smiles/" + add;
            if (type == 5)
            {
                Regex extsmile1 = new Regex(@":-?@", m_options); // :@
                Regex extsmile2 = new Regex(@"%-?\)", m_options); // %)
                Regex extsmile3 = new Regex(@"(:sad:)|(:с[аэ]д:)|(:печаль:)", m_options); // :sad:
                Regex extsmile4 = new Regex(@"(:nono:)|(:ноно:)", m_options); // :nono:
                Regex extsmile5 = new Regex(@"(:lame:)|(:ламер?:)", m_options); // :lame:
                Regex extsmile6 = new Regex(@"(:deer:)|(:олень:)|(:деер:)", m_options); // :deer:
                Regex extsmile7 = new Regex(@"(:clown:)|(:кловн:)|(:клоун:)", m_options); // :clown:

                Regex extsmile8 = new Regex(@"(:hack:)|(:хаск:)|(:хак:)", m_options); // :hack:
                Regex extsmile9 = new Regex(@":-/", m_options); // :-/
                Regex extsmile10 = new Regex(@"(:overlook:)|(:оверлоок:)|(:оверлук:)", m_options); // :overlook:
                Regex extsmile11 = new Regex(@"(:rus:)|(:рус:)", m_options); // :рус:

                Regex extsmile12 = new Regex(@"(:hm:)|(:хм:)", m_options); // :hm:
                Regex extsmile13 = new Regex(@"(:satisfed:)|(:сатисфед:)", m_options); // :satisfed:
                Regex extsmile14 = new Regex(@"(:dk2:)|(:дк2:)", m_options); // :dk2:
                Regex extsmile15 = new Regex(@"(:confuse:)|(:сонфусе:)|(:конфуз:)", m_options); // :confuse:
                Regex extsmile16 = new Regex(@"(:amaze:)|(:амазе:)", m_options); // :amaze:
                Regex extsmile17 = new Regex(@"(:angry:)|(:ангру:)", m_options); // :angry:

                if (count < max) { count += NestedReplace(ref s, extsmile1, smilesDir + "1" + ext + "\" alt=\":@\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, extsmile2, smilesDir + "2" + ext + "\" alt=\"%)\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, extsmile3, smilesDir + "113" + ext + "\" alt=\"sad\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, extsmile4, smilesDir + "114" + ext + "\" alt=\"nono\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, extsmile5, smilesDir + "115" + ext + "\" alt=\"lame\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, extsmile6, smilesDir + "116" + ext + "\" alt=\"deer\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, extsmile7, smilesDir + "117" + ext + "\" alt=\"clown\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, extsmile8, smilesDir + "ez" + ext + "\" alt=\"hack\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, extsmile9, smilesDir + "ex" + ext + "\" alt=\":-/\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, extsmile10, smilesDir + "ew" + ext + "\" alt=\"overlook\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, extsmile11, smilesDir + "db" + ext + "\" alt=\"rus\" />", max - count); }

                if (count < max) { count += NestedReplace(ref s, extsmile12, smilesDir + "ss1" + ext + "\" alt=\"hm\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, extsmile13, smilesDir + "ss2" + ext + "\" alt=\"satisfed\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, extsmile14, smilesDir + "ss3" + ext + "\" alt=\"dk2\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, extsmile15, smilesDir + "ss4" + ext + "\" alt=\"confuse\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, extsmile16, smilesDir + "ss5" + ext + "\" alt=\"amaze\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, extsmile17, smilesDir + "ss6" + ext + "\" alt=\"angry\" />", max - count); }
            }
            if (((type > 0) && (type <= 2)) || (type >= 4))
            {
                if (count < max) { count += NestedReplace(ref s, smile78, smilesDir + "cv" + ext + "\" alt=\"ninj\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, smile77, smilesDir + "ev" + ext + "\" alt=\"rab\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, smile76, smilesDir + "ea" + ext + "\" alt=\"g_in_love\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, smile74, smilesDir + "dz" + ext + "\" alt=\"hospital\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, smile72, smilesDir + "bw" + ext + "\" alt=\"censored\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, smile71, smilesDir + "dx" + ext + "\" alt=\"girl_cry\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, smile70, smilesDir + "eb" + ext + "\" alt=\"hysteric\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, smile69, smilesDir + "dy" + ext + "\" alt=\"g_crazy\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, smile68, smilesDir + "ed" + ext + "\" alt=\"g_drink\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, smile67, smilesDir + "el" + ext + "\" alt=\"flowers\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, smile65, smilesDir + "cy" + ext + "\" alt=\"punish\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, smile64, smilesDir + "bl" + ext + "\" alt=\"friend\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, smile63, smilesDir + "as" + ext + "\" alt=\"inlove\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, smile62, smilesDir + "aq" + ext + "\" alt=\"diablo\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, smile61, smilesDir + "ec" + ext + "\" alt=\"tender\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, smile60, smilesDir + "ee" + ext + "\" alt=\"imposs\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, smile59, smilesDir + "eh" + ext + "\" alt=\"wizard\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, smile58, smilesDir + "dw" + ext + "\" alt=\"hunter\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, smile57, smilesDir + "ek" + ext + "\" alt=\"bokaly\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, smile56, smilesDir + "es" + ext + "\" alt=\"roller\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, smile55, smilesDir + "ar" + ext + "\" alt=\"yahoo\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, smile54, smilesDir + "de" + ext + "\" alt=\"think\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, smile53, smilesDir + "au" + ext + "\" alt=\"tired\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, smile52, smilesDir + "bm" + ext + "\" alt=\"sleep\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, smile51, smilesDir + "cc" + ext + "\" alt=\"flirt\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, smile50, smilesDir + "dn" + ext + "\" alt=\"tease\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, smile49, smilesDir + "do" + ext + "\" alt=\"smoke\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, smile48, smilesDir + "cl" + ext + "\" alt=\"distruth\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, smile47, smilesDir + "ba" + ext + "\" alt=\"heart\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, smile46, smilesDir + "cg" + ext + "\" alt=\"crazy\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, smile45, smilesDir + "az" + ext + "\" alt=\"drink\" />", max - count); }
                //if (count < max) { count += NestedReplace(ref s, smile43, smilesDir + "em" + ext + "\" alt=\"vedma\" />", max - count); }
                //if (count < max) { count += NestedReplace(ref s, smile42, smilesDir + "ep" + ext + "\" alt=\"g_emo\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, smile40, smilesDir + "ef" + ext + "\" alt=\"sigh\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, smile39, smilesDir + "du" + ext + "\" alt=\"sarc\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, smile38, smilesDir + "av" + ext + "\" alt=\"stop\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, smile37, smilesDir + "bh" + ext + "\" alt=\"sory\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, smile36, smilesDir + "bv" + ext + "\" alt=\"song\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, smile35, smilesDir + "cz" + ext + "\" alt=\"read\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, smile34, smilesDir + "ax" + ext + "\" alt=\"rose\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, smile32, smilesDir + "af" + ext + "\" alt=\"rofl\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, smile31, smilesDir + "bp" + ext + "\" alt=\"rtfm\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, smile30, smilesDir + "bb" + ext + "\" alt=\"bomb\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, smile29, smilesDir + "cx" + ext + "\" alt=\"kiss\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, smile28, smilesDir + "cp" + ext + "\" alt=\"king\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, smile27, smilesDir + "cj" + ext + "\" alt=\"fear\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, smile26, smilesDir + "dt" + ext + "\" alt=\">:O\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, smile25, smilesDir + "ay" + ext + "\" alt=\"yes\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, smile24, smilesDir + "bc" + ext + "\" alt=\"sos\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, smile22, smilesDir + "ck" + ext + "\" alt=\"fm\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, smile21, smilesDir + "cu" + ext + "\" alt=\"su\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, smile20, smilesDir + "al" + ext + "\" alt=\"dk\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, smile19, smilesDir + "bf" + ext + "\" alt=\"ok\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, smile18, smilesDir + "er" + ext + "\" alt=\"hi\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, smile17, smilesDir + "bd" + ext + "\" alt=\"\\m/\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, smile16, smilesDir + "ai" + ext + "\" alt=\"=-O\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, smile15, smilesDir + "ak" + ext + "\" alt=\":'(\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, smile14, smilesDir + "ap" + ext + "\" alt=\": ))\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, smile13, smilesDir + "ab" + ext + "\" alt=\":(\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, smile12, smilesDir + "ac" + ext + "\" alt=\";)\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, smile11, smilesDir + "ad" + ext + "\" alt=\":P\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, smile10, smilesDir + "ae" + ext + "\" alt=\"8-)\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, smile9, smilesDir + "ah" + ext + "\" alt=\":[\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, smile8, smilesDir + "aw" + ext + "\" alt=\":*\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, smile7, smilesDir + "am" + ext + "\" alt=\":|\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, smile6, smilesDir + "df" + ext + "\" alt=\":x\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, smile5, smilesDir + "ao" + ext + "\" alt=\":\\\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, smile4, smilesDir + "at" + ext + "\" alt=\":!\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, smile3, smilesDir + "aa" + ext + "\" alt=\":)\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, smile2, smilesDir + "bs" + ext + "\" alt=\";D\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, smile1, smilesDir + "ei" + ext + "\" alt=\":D\" />", max - count); }
            }
            if ((type > 2) && (type <= 3))
            {
                if (count < max) { count += NestedReplace(ref s, smile32, smilesDir + "af" + ext + "\" alt=\"rofl\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, smile12, smilesDir + "ac" + ext + "\" alt=\";)\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, smile20, smilesDir + "al" + ext + "\" alt=\"dk\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, smile14, smilesDir + "ap" + ext + "\" alt=\": ))\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, smile3, smilesDir + "aa" + ext + "\" alt=\":)\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, smile13, smilesDir + "ab" + ext + "\" alt=\":(\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, smile12, smilesDir + "ac" + ext + "\" alt=\";)\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, smile10, smilesDir + "ae" + ext + "\" alt=\"8-)\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, smile11, smilesDir + "ad" + ext + "\" alt=\":P\" />", max - count); }
                if (count < max) { count += NestedReplace(ref s, smile16, smilesDir + "ai" + ext + "\" alt=\"=-O\" />", max - count); }
                if (count < max) { NestedReplace(ref s, smile1, smilesDir + "ei" + ext + "\" alt=\":D\" />", max - count); }
            }
            return s;
        }

        public static string Cutstring(string s, int min, int max)
        {
            string res = "";
            int runto = s.Length;
            if (s.Length > max) runto = max;
            for (int i = 0; i < runto; i++)
            {
                if (i > min)
                {
                    if ((s[i] == ' ') || (s[i] == '_') || (s[i] == '-'))
                    {
                        break;
                    }
                }
                res += s[i];
            }
            if (s.Length > max) res += "...";
            return res;
        }

        public static string GetDateDiff(DateTime dt)
        {
            return GetDateDiff(dt, DateTime.Now);
        }

        public static string GetDateDiff(DateTime dt, DateTime now)
        {
            string result = "";
            int count = 0;
            if (now.AddMinutes(-1).CompareTo(dt) == -1)
            {
                count = (int)now.Subtract(dt).TotalSeconds;
                result = count + " ";
                if (count == 11 || count == 12) result += "секунд";
                else if ((count % 10) >= 2 && (count % 10 <= 4)) result += "секунды";
                else if ((count % 10) >= 1 && (count % 10 <= 1)) result += "секунду";
                else if ((count % 10) >= 5 && (count % 10 <= 9)) result += "секунд";
                else if ((count % 10) >= 0 && (count % 10 <= 0)) result += "секунд";

            }
            else if (now.AddHours(-1).CompareTo(dt) == -1)
            {
                count = (int)now.Subtract(dt).TotalMinutes;
                result = count + " ";
                if (count == 11 || count == 12) result += "минут";
                else if ((count % 10) >= 2 && (count % 10 <= 4)) result += "минуты";
                else if ((count % 10) >= 1 && (count % 10 <= 1)) result += "минуту";
                else if ((count % 10) >= 5 && (count % 10 <= 9)) result += "минут";
                else if ((count % 10) >= 0 && (count % 10 <= 0)) result += "минут";
            }
            else if (now.AddDays(-1).CompareTo(dt) == -1)
            {
                count = (int)now.Subtract(dt).TotalHours;
                result = count + " ";
                if (count == 11 || count == 12) result += "часов";
                else if ((count % 10) >= 2 && (count % 10 <= 4)) result += "часа";
                else if ((count % 10) >= 1 && (count % 10 <= 1)) result += "час";
                else if ((count % 10) >= 5 && (count % 10 <= 9)) result += "часов";
                else if ((count % 10) >= 0 && (count % 10 <= 0)) result += "часов";
            }
            else
            {
                count = (int)now.Subtract(dt).TotalDays;
                result = count + " ";
                if (count == 11 || count == 12) result += "дней";
                else if ((count % 10) >= 2 && (count % 10 <= 4)) result += "дня";
                else if ((count % 10) >= 1 && (count % 10 <= 1)) result += "день";
                else if ((count % 10) >= 5 && (count % 10 <= 9)) result += "дней";
                else if ((count % 10) >= 0 && (count % 10 <= 0)) result += "дней";
            }
            result += " назад";
            return result;
        }
    }
}